import request from '../request'
import type { ApiResponse, PageData } from '../types'
import type { Order } from '@/types/Orders'

// PageData 已在 api/types.ts 中统一声明

// 获取订单列表（分页）
export async function getOrders(params: {
  // userId: number;
  // userName: string;
  pageNum: number;
  pageSize: number;
  orderNo?: string;       
  paymentMethod?: string;  
}) {
  return request<ApiResponse<PageData<Order>>>({
    url: '/orders/my',
    method: 'GET',
    
    params, // 使用 params 发送分页及搜索参数（正确）
    // headers: { 'userId': params.userId ,'userName': params.userName} 
  });
}

// 获取订单详情
export async function getOrder(orderNo: string) {
  return request<ApiResponse<any>>({
    url: `/orders/${orderNo}`,
    method: 'GET'
  })
}

// 创建订单
export async function createOrder(data: {
  type: 'COURSE' | 'TEST' | 'CONSULTATION'
  itemId: number
  couponId?: number | null
  remark?: string | null
}) {
  return request<ApiResponse<string>>({
    url: '/orders',
    method: 'POST',
    data
  })
}

// 支付订单
export async function payOrder(orderNo: string, data: {
  paymentMethod: 'ALIPAY' | 'WECHAT' | 'BALANCE'
  returnUrl?: string
  notifyUrl?: string
}) {
  return request<ApiResponse<{
    paymentUrl?: string
    orderNo: string
    paymentMethod: string
    message: string
    needRedirect: boolean
  }>>({
    url: `/orders/${orderNo}/pay`,
    method: 'POST',
    data
  })
}

// 取消订单
export async function cancelOrder(orderId: number, data?: {
  reason?: string | null
}) {
  return request<ApiResponse<any>>({
    url: `/orders/${orderId}/cancel`,
    method: 'PUT',
    data
  })
}